﻿#include <QCoreApplication>
#include <stdlib.h>
#include <iostream>
using namespace std;
#include <windows.h>
#include <QString>

#include <help.h>
#include <text_info.h>
#include <config.h>
#include <start.h>

QList<QString> first_arg=QList<QString>()<<"help"<<"start"<<"config";



void exec_cmd()
{
    cout<<version_text.toStdString()<<endl;
}

void exec_cmd(QString arg1)
{
    int case_id = -1;

    for(int i=0; i < first_arg.count();i++)
    {
        if (first_arg.at(i) == arg1)
            case_id = i;
    }

    switch (case_id) {
        case 0:cout<<help_text.toStdString()<<endl; break;
        case 1:cout<<start_text.toStdString()<<endl; break;
        case 2:show_config(); break;
        default:cout<<"无效命令:"<<arg1.toStdString()<<error_text.toStdString()<<endl; break;
    }
}
void exec_cmd(QString arg1,QString arg2)
{
//    QString qs_arg2 = QString::fromLocal8Bit(arg2); //命令行默认传入的编码格式为GBK,这里将GBK转为Unicode(QString 默认使用Unicode编码)
//    cout << qs_arg2.toStdString()<<endl;
    int case_id = -1;
    for(int i=0; i < first_arg.count();i++)
    {
        if (first_arg.at(i) == arg1)
            case_id = i;
    }

    switch (case_id) {
        case 0:help(arg2); break;
        case 1:start(arg2); break;
        case 2:config(arg2); break;
        default:cout<<"无效命令:"<<arg1.toStdString()<<error_text.toStdString()<<endl; break;
    }
}

void exec_cmd(QString  arg1, QString  arg2, QString arg3)
{
//    QString qs_arg2 = QString::fromLocal8Bit(arg2); //命令行默认传入的编码格式为GBK,这里将GBK转为Unicode(QString 默认使用Unicode编码)
//    QString qs_arg3 = QString::fromLocal8Bit(arg3); //命令行默认传入的编码格式为GBK,这里将GBK转为Unicode(QString 默认使用Unicode编码)
    int case_id = -1;
    for(int i=0; i < first_arg.count();i++)
    {
        if (first_arg.at(i) == arg1)
            case_id = i;
    }

    switch (case_id) {
        case 0:cout<<"无效参数:"<<arg3.toStdString()<<error_text.toStdString()<<endl; break;
        case 1:start(arg2, arg3); break;
        case 2:cout<<"无效参数:"<<arg3.toStdString()<<error_text.toStdString()<<endl; break;
        default:cout<<error_text.toStdString()<<endl; break;
    }
}



int main(int argc, char *argv[])
{
    SetConsoleOutputCP(65001); //转为UTF-8编码显示
    QCoreApplication a(argc, argv);

    ///命令行默认传入的编码格式为GBK,这里将GBK转为Unicode(QString 默认使用Unicode编码)
    QStringList args;
    for(int i = 0 ;i <argc ;i++)
    {
        QString arg = QString::fromLocal8Bit(argv[i]);
        args.append(arg);
//        cout<< "\n\nargv[" <<i << "] = "<< argv[i] <<"\n";
    }


    //初始化文本信息
    init_text();

    //初始化配置信息
    init_config();


    switch (argc)
    {
        case 1:exec_cmd();break;
        case 2:exec_cmd(args[1]);break;
        case 3:exec_cmd(args[1],args[2]);break;
        case 4:exec_cmd(args[1],args[2],args[3]);break;

        default:
            cout<<"命令错误!请通过help查看。"<<endl;
            exec_cmd();
            break;
    }


//    for(int i = 0 ;i <argc ;i++)
//    {
//        cout<< "\n\nargv[" <<i << "] = "<< argv[i] <<"\n";
//    }

//    cout << "argc:" << argc <<endl;

    return 0;
//    return a.exec();

}
